package at.fhj.itm11.forum.service.stubs;

import java.util.HashSet;
import java.util.Set;

import at.fhj.itm11.forum.data.RatingDAO;
import at.fhj.itm11.forum.model.Rating;

public class RatingDAOStub implements RatingDAO {

	public Set<Rating> ratings = new HashSet<Rating>();
	
	@Override
	public Rating findById(Long id) {
		for(Rating r : ratings) {
			if(r.getId().equals(id)) {
				return r;
			}
		}
		
		return null;
	}

	@Override
	public void rate(Rating r) {
		ratings.add(r);
		
	}

	@Override
	public Rating findRatingByRaterAndPost(Long userId, Long postId) {
		for(Rating r : ratings) {
			if(r.getUser().getId().equals(userId) && r.getPost().getId().equals(postId)) {
				return r;
			}
		}
		
		return null;
	}

	@Override
	public Long findPositiveRatingCountForPost(Long postId) {
		Long numberOfPositiveRatings = 0L;
		
		for(Rating r : ratings) {
			if(r.getPost().getId().equals(postId) && r.isPositiveRating()) {
				numberOfPositiveRatings++;
			}
		}
		
		return numberOfPositiveRatings;
	}

	@Override
	public Long findNegativeRatingCountForPost(Long postId) {
		Long numberofNegativeRatings = 0L;
		
		for(Rating r : ratings) {
			if(r.getPost().getId().equals(postId) && !r.isPositiveRating()) {
				numberofNegativeRatings++;
			}
		}
		
		return numberofNegativeRatings;
	}

}
